import re

def extract_data(file_line:str,pattern='(?P<ip>[\d.]{7,}) - - \[(.+)\] "(.+) (?P<url>.+) (.+)" (\d{3}) (\d+) "-" "([^"]+)"'):
    regex = re.compile(pattern)
    rex = regex.match(file_line)
    return rex.groupdict() if rex else None

def loadfile(file_name:str,encoding='utf-8'):
    with open(file_name,encoding=encoding) as f:
        for line in f:
            fields = extract_data(line)
            if fields:
                yield fields
            else:
                continue

def find_data(path):
    ip_dict = {}
    url_dict = {}
    for d in loadfile(path):
        for k,v in d.items():
            if k == 'ip' :
                ip_dict[v] = ip_dict.get(v,0) + 1
            elif k == 'url' :
                url_dict[v] = url_dict.get(v, 0) + 1
    sort_ip = sorted(ip_dict.keys(),key=lambda x:ip_dict[x],reverse=True)
    sort_url = sorted(url_dict.keys(),key=lambda  x:url_dict[x],reverse=True)

    return sort_ip[:10],sort_url[:10]
ip,url = find_data('./access.log-20191119')
print(ip)
print(url)


# ip排序的结果是对的，但是url排序结果不对。你在得到对应的结果以后可以自己验证一下。
# 使用notepad++打开access.log文件，然后使用查找功能，查找对应的url结果，看它的计数和你
# 输出的结果中是否一样。